package com.java110.mall.goods.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.dto.allocationOrder.AllocationOrderDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.mall.goods.bmo.allocationOrder.IDeleteAllocationOrderBMO;
import com.java110.mall.goods.bmo.allocationOrder.IGetAllocationOrderBMO;
import com.java110.mall.goods.bmo.allocationOrder.IUpdateAllocationOrderBMO;
import com.java110.intf.IAllocationOrderInnerServiceSMO;
import com.java110.intf.goods.IProductSpecValueInnerServiceSMO;
import com.java110.po.account.AllocationOrderPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/allocationOrder")
public class AllocationOrderApi {


    @Autowired
    private IUpdateAllocationOrderBMO updateAllocationOrderBMOImpl;

    @Autowired
    private IDeleteAllocationOrderBMO deleteAllocationOrderBMOImpl;

    @Autowired
    private IGetAllocationOrderBMO getAllocationOrderBMOImpl;

    @Autowired
    private IProductSpecValueInnerServiceSMO productSpecValueInnerServiceSMOImpl;

    @Autowired
    private IAllocationOrderInnerServiceSMO allocationOrderInnerServiceSMOImpl;


    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /allocationOrder/updateAllocationOrder
     * @path /app/allocationOrder/updateAllocationOrder
     */
    @RequestMapping(value = "/updateAllocationOrder", method = RequestMethod.POST)
    public ResponseEntity<String> updateAllocationOrder(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "shopName", "请求报文中未包含shopName");
        Assert.hasKeyAndValue(reqJson, "srcShName", "请求报文中未包含srcShName");
        Assert.hasKeyAndValue(reqJson, "targetShName", "请求报文中未包含targetShName");
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");
        ProductSpecValueDto productSpecValueDto = new ProductSpecValueDto();
        productSpecValueDto.setProductId(reqJson.getString("productId"));
        productSpecValueDto.setSpecId(reqJson.getString("specId"));
        List<ProductSpecValueDto> productSpecValueDtos = productSpecValueInnerServiceSMOImpl.queryProductSpecValues(productSpecValueDto);
        Assert.listOnlyOne(productSpecValueDtos, "查询规格值表错误");
        //获取库存
        String stock = productSpecValueDtos.get(0).getStock();
        AllocationOrderDto allocationOrderDto = new AllocationOrderDto();
        allocationOrderDto.setOrderId(reqJson.getString("orderId"));
        List<AllocationOrderDto> allocationOrderDtos = allocationOrderInnerServiceSMOImpl.queryAllocationOrders(allocationOrderDto);
        Assert.listOnlyOne(allocationOrderDtos, "查询调拨表错误");
        //获取修改前调拨数量
        String productStock = allocationOrderDtos.get(0).getProductCount();
        //计算调拨前总数量
        double count = Double.parseDouble(stock) + Double.parseDouble(productStock);
        //获取调拨数量
        String productCount = reqJson.getString("productCount");
        if (count < Double.parseDouble(productCount)) {
            throw new IllegalArgumentException("库存不足！");
        }
        //获取修改调拨数量后剩余的库存
        double liveCount = count - Double.parseDouble(productCount);
        AllocationOrderPo allocationOrderPo = BeanConvertUtil.covertBean(reqJson, AllocationOrderPo.class);
        allocationOrderPo.setValueId(productSpecValueDtos.get(0).getValueId());
        allocationOrderPo.setStock(String.valueOf(liveCount));
        return updateAllocationOrderBMOImpl.update(allocationOrderPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /allocationOrder/deleteAllocationOrder
     * @path /app/allocationOrder/deleteAllocationOrder
     */
    @RequestMapping(value = "/deleteAllocationOrder", method = RequestMethod.POST)
    public ResponseEntity<String> deleteAllocationOrder(@RequestBody JSONObject reqJson) {
//        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");
        AllocationOrderPo allocationOrderPo = BeanConvertUtil.covertBean(reqJson, AllocationOrderPo.class);
        return deleteAllocationOrderBMOImpl.delete(allocationOrderPo);
    }

    /**
     * 微信查询消息模板
     *
     * @param shopId 商铺ID
     * @return
     * @serviceCode /allocationOrder/queryAllocationOrder
     * @path /app/allocationOrder/queryAllocationOrder
     */
    @RequestMapping(value = "/queryAllocationOrder", method = RequestMethod.GET)
    public ResponseEntity<String> queryAllocationOrder(@RequestParam(value = "shopId", required = false) String shopId,
                                                       @RequestParam(value = "srcShId", required = false) String srcShId,
                                                       @RequestParam(value = "targetShId", required = false) String targetShId,
                                                       @RequestParam(value = "state", required = false) String state,
                                                       @RequestParam(value = "orderId", required = false) String orderId,
                                                       @RequestParam(value = "page") int page,
                                                       @RequestParam(value = "row") int row) {
        AllocationOrderDto allocationOrderDto = new AllocationOrderDto();
        allocationOrderDto.setPage(page);
        allocationOrderDto.setRow(row);
        allocationOrderDto.setShopId(shopId);
        allocationOrderDto.setOrderId(orderId);
        allocationOrderDto.setSrcShId(srcShId);
        allocationOrderDto.setTargetShId(targetShId);
        allocationOrderDto.setState(state);
        return getAllocationOrderBMOImpl.get(allocationOrderDto);
    }

    /**
     * 微信审核消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode
     * @path /app/allocationOrder/auditAllocationOrder
     */
    @RequestMapping(value = "/auditAllocationOrder", method = RequestMethod.POST)
    public ResponseEntity<String> auditAllocationOrder(@RequestBody JSONObject reqJson) {
//        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");
        AllocationOrderPo allocationOrderPo = BeanConvertUtil.covertBean(reqJson, AllocationOrderPo.class);
        return updateAllocationOrderBMOImpl.update(allocationOrderPo);
    }

    /**
     * 微信入库消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /allocationOrder/warehousingAllocationOrder
     * @path /app/allocationOrder/warehousingAllocationOrder
     */
    @RequestMapping(value = "/warehousingAllocationOrder", method = RequestMethod.POST)
    public ResponseEntity<String> warehousingAllocationOrder(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "orderId", "orderId不能为空");
        AllocationOrderPo allocationOrderPo = BeanConvertUtil.covertBean(reqJson, AllocationOrderPo.class);
        return updateAllocationOrderBMOImpl.update(allocationOrderPo);
    }
}
